package org.jeecgframework.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.ZyOpinionServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyOpinionEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Title: Controller
 * @Description: 意见表
 * @author onlineGenerator
 * @date 2016-05-27 15:43:02
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/zyOpinionController")
public class ZyOpinionController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZyOpinionController.class);

	@Autowired
	private ZyOpinionServiceI zyOpinionService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 意见表列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "zyOpinion")
	public ModelAndView zyOpinion(HttpServletRequest request) {
		TSUser user = ResourceUtil.getSessionUserName();
		String orgCode = user.getCurrentDepart().getOrgCode();
		if(!orgCode.startsWith(StaticValue.ORG_CODE_BARCO)){
			request.setAttribute("add", "1");
		}
		return new ModelAndView("com/zybk/zy_opinion/zyOpinionList");
	}

	/**
	 * easyui AJAX请求数据
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(ZyOpinionEntity zyOpinion,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZyOpinionEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zyOpinion, request.getParameterMap());
		try{
			TSUser user = ResourceUtil.getSessionUserName();
			String orgCode = user.getCurrentDepart().getOrgCode();
			if(!orgCode.startsWith(StaticValue.ORG_CODE_BARCO)){
				cq.add(Restrictions.eq("createBy",user.getUserName()));
			}
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.zyOpinionService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 意见表新增页面跳转
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(HttpServletRequest req) {
		return new ModelAndView("com/zybk/zy_opinion/zyOpinion-add");
	}
	

	/**
	 * 添加意见表
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(ZyOpinionEntity zyOpinion, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "意见或建议添加成功";
		try{
			zyOpinionService.save(zyOpinion);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "意见或建议添加成功";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 意见表查看页面跳转
	 */
	@RequestMapping(params = "goView")
	public ModelAndView goView(HttpServletRequest req,@RequestParam(value="id")String id) {
		ZyOpinionEntity zyOpinionEntity = zyOpinionService.get(ZyOpinionEntity.class, id);
		req.setAttribute("zyOpinionEntity", zyOpinionEntity);
		return new ModelAndView("com/zybk/zy_opinion/zyOpinion-view");
	}
	
}
